import 'dart:io';
import 'package:flutter_hsh/fluttergromore/ads/ads_utils.dart';
import 'package:flutter_hsh/fluttergromore/usersp/user_sp.dart';
import 'package:flutter_hsh/routes/app_pages.dart';
import 'package:get/get.dart';
import '../../fluttergromore/config/app_config.dart';
import '../../fluttergromore/config/app_const.dart';
import '../../fluttergromore/event_bus/event_bus_utils.dart';
import '../../fluttergromore/plugin/ts_plugin.dart';
import '../../fluttergromore/privacy/privacy_widget.dart';

class VCSplashLogic extends GetxController {
  int splashCount = 0;

  //自增ID
  RxString riseId = "".obs;

  @override
  void onInit() {
    super.onInit();
    initData();
  }

  @override
  void onReady() {
    super.onReady();
    //初始化
    init();
    // initNoAd();
  }

  //初始化数据
  void initData() async {
    riseId.value = await UserSp.instance.getRiseId() ?? "";
    //开屏事件回调
    eventBusUtils.kpSucceedOn((e) {
      //如果是CSJ
      // if (AppConst.isCSJ) {
      //   toNextPage();
      // } else {
      //   //toNextPage();
      //   if (splashCount == 1) {
      //     toNextPage();
      //   } else {
      //     AdsUtils.showSplashAd();
      //     splashCount++;
      //   }
      //   // //存开屏的次数
      //   // var count = await UserSp.instance.getKpCount();
      //   // UserSp.instance.putKpCount(count + splashCount);
      //   // if (count >= 4) {
      //   //   UserSp.instance.putKpCountTime(DateTime.now().millisecondsSinceEpoch);
      //   // }
      // }
      toNextPage();
    });
  }

  void init() async {
    bool isAgreePrivacy = await UserSp().isPrivacyAccept();
    // //是否同意隐私协议
    if (isAgreePrivacy) {
      initADS();
    } else {
      onShowPrivacyDialog();
    }
  }

  void initADS() {
    //初始化广告配置
    AppConfig.initAds(
      onNextPage: () {
        toNextPage();
      },
      onRiseId: (data) async {
        riseId.value = data != null && data != 0
            ? data.toString()
            : await UserSp.instance.getRiseId() ?? "";
      },
    );
  }

  //隐私弹窗
  Future<void> onShowPrivacyDialog() async {
    Get.dialog(
        barrierDismissible: false,
        PrivacyWidget(
          onTapNo: () {
            exit(0);
          },
          onTapYes: () {
            UserSp().putPrivacyAccept(true);
            TsPlugin.privacyAcceptTrue();
            initADS();
          },
        ));
  }

  //不初始化广告的方法
  Future<void> initNoAd() async {
    bool isAgreePrivacy = await UserSp().isPrivacyAccept();
    // //是否同意隐私协议
    if (isAgreePrivacy) {
      toNextPage();
    } else {
      Get.dialog(
        barrierDismissible: false,
        PrivacyWidget(
          onTapNo: () {
            exit(0);
          },
          onTapYes: () {
            UserSp().putPrivacyAccept(true);
            toNextPage();
          },
        ),
      );
    }
  }

  void toNextPage() async {
    await Future.delayed(const Duration(seconds: 2));
    Get.offAllNamed(Routes.main);
  }

  @override
  void onClose() {
    //移除开屏监听
    eventBusUtils.kpSucceedOff();
    super.onClose();
  }
}
